﻿using System.Collections.Generic;
using System.Linq;
using CardPlay.Model.CardModel;
using CardPlay.Model.GameModel;
using CardPlay.Model.GameModel.Presentation;
using CardPlay.Model.Layout;

namespace CardPlay.DemoPack.Games.PairingVariants
{
    [Game("EA4CFD72-9FE5-4F40-8A57-7033F0B4C3FB", LevelOfDifficulty.Easy, LevelOfSkill.MostlyChance, DurationOfPlay.Within5To10Minutes, Family = FamilyName)]
    public class Nestor : PairingStyleGame
    {
        public Nestor()
        {
            TotalTableaus = 8;
            TotalReserves = 4;
            TotalReserveRows = 1;

            NamesOfAdditionalPiles.AddRange(NamesOfReserves);
        }

        public override ILayoutSite CreatePresentation(IPresentationContext presentation)
        {
            var reserveStyle = new VisualTraits
            {
                Arrangement = CardArrangement.StackTopToBottom,
                ContentFit = ContentFit.FitBounds
            };

            var root = presentation.CreateColumnLayout();

            var reservesPerRow = TotalReserves / TotalReserveRows;

            for (var i = 0; i < TotalReserveRows; ++i)
            {
                var reserveRow = presentation.CreateRowLayout();
                root.Add(reserveRow);

                reserveRow.HorizontalContentAlignment = HorizontalAlignment.Center;
                presentation.AddPiles(reserveRow, NamesOfReserves.Skip(i * reservesPerRow).Take(reservesPerRow), reserveStyle);
            }

            var tableauRow = presentation.CreateRowLayout();
            root.Add(tableauRow);

            presentation.AddPiles(tableauRow, NamesOfTableaus, reserveStyle);

            return root;
        }

        protected override bool IsOver
        {
            get { return !SetHasPair(this.NonEmptyPilesNamed(AnyTableau + "|" + AnyReserve)); }
        }

        protected override IDealAction CreateDealScript()
        {
            var sequence = new DealSequence();

            foreach (var pile in PilesNamed(AnyTableau))
            {
                var target = pile;
                sequence.Add(new DealCardAction(target.Name, face => !target.Contains(face.Rank)) { Orientation = CardOrientation.FaceUp, Repeat = 6 });
            }

            foreach (var action in NamesOfReserves.Select(name => new DealCardAction(name) { Orientation = CardOrientation.FaceUp }))
                sequence.Add(action);

            return sequence;
        }

        protected override void PrepareDealScript(DealScript script)
        {
        }

        protected int TotalReserves { get; set; }

        protected int TotalReserveRows { get; set; }

        protected IEnumerable<string> NamesOfReserves
        {
            get { return StandardPileNames.Reserves.Take(TotalReserves); }
        }
    }
}
